package com.woniu.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {

    @Bean
    public Exchange complainMessageExchange(){
        return new DirectExchange("complain-message-exchange",true,false);
    }

    @Bean
    public Queue complainQueue(){
        return new Queue("complain-message-queue");
    }

    @Bean
    public Binding complainBinding(@Qualifier("complainQueue") Queue queue,
                                   @Qualifier("complainMessageExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("message").noargs();
    }

    @Bean
    public Exchange ComplainConstExchange(){
        return new DirectExchange("complain-const-exchange",true,false);
    }

    @Bean()
    public Queue ComplainConstQueue(){
        Map<String,Object> params = new HashMap<>();
        params.put("x-message-ttl",50000);
        params.put("x-dead-letter-exchange","exchange-dead");
        params.put("x-dead-letter-routing-key","dlx.ooo");
        //队列名称、队列持久化、是否独占队列、队列不使用自动删除、队列属性
        return new Queue("complian-const-queue",true,false,false, params);
    }

    @Bean
    public Binding complainConstBinding(@Qualifier("ComplainConstQueue") Queue queue,
                                   @Qualifier("ComplainConstExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("message").noargs();
    }



    @Bean("exchangeDead")
    public Exchange exchangeDead(){
        return new TopicExchange("exchange-dead",true,false);
    }

    @Bean("queueDead")
    public Queue queueDead() {
        return new Queue("queue-dead",true,false,false);
    }

    @Bean
    public Binding bindDlxQueue(
            @Qualifier("exchangeDead") Exchange exchange,
            @Qualifier("queueDead") Queue queue) {
        return BindingBuilder
                .bind(queue)
                .to(exchange).with("dlx.#").noargs();
    }

    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public RabbitAdmin rabbitAdmin(
            ConnectionFactory factory,
            Exchange complainMessageExchange,
            Exchange ComplainConstExchange,
            Exchange exchangeDead,
            Queue ComplainConstQueue,
            Queue complainQueue,
            Queue queueDead) {
        RabbitAdmin rabbitAdmin = new RabbitAdmin(factory);
        rabbitAdmin.setAutoStartup(true);
        rabbitAdmin.declareExchange(complainMessageExchange);
        rabbitAdmin.declareExchange(exchangeDead);
        rabbitAdmin.declareExchange(ComplainConstExchange);
        rabbitAdmin.declareQueue(complainQueue);
        rabbitAdmin.declareQueue(queueDead);
        rabbitAdmin.declareQueue(ComplainConstQueue);
        return rabbitAdmin;
    }

}
